home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / pcxkit.exe / PCX.PAS < prev    next >
Pascal/Delphi Source File  |  1992-03-26  |  24KB  |  628 lines

  1. unit PCX;
  2.  
  3. (* version 3.1
  4.                              by Peter Donnelly
  5.                               1301 Ryan Street
  6.                                 Victoria BC
  7.                                Canada V8T 4Y8
  8.  
  9.    ╒══════════════════════════════════════════════════════════════════════╕
  10.    │    May be copied freely. If you make practical use of this unit,     │
  11.    │         a contribution of $10 or more would be appreciated.          │     
  12.    ╘══════════════════════════════════════════════════════════════════════╛
  13.  
  14.    This is a unit to read .PCX files and put them in displayable form. The
  15.    actual work of decoding the file and moving the data into memory is done
  16.    in assembler. Version 6 of Turbo Pascal is required for compilation.
  17.  
  18.    The following display modes are supported:
  19.  
  20.           Mode      TP GraphMode     Resolution    Colors
  21.           ~~~~      ~~~~~~~~~~~~     ~~~~~~~~~~    ~~~~~~
  22.           $04       CGAC0 to C3      320 x 200         4
  23.           $06       CGAHi            640 x 200         2
  24.           $0D        ---             320 x 200        16
  25.           $0E       EGALo/VGALo      640 x 200        16
  26.           $10       EGAHi/VGAMed     640 x 350        16
  27.           $12       VGAHi            640 x 480        16
  28.           $13        ---             320 x 200       256
  29.  
  30.    Mode $13 is supported only for files containing palette information,
  31.    i.e. not those produced by versions of Paintbrush earlier than 3.0.
  32.  
  33.    The unit has been optimized for speed rather than flexibility or
  34.    compactness. In particular, the routine for displaying 16-color files
  35.    (which require the most computation) has been improved and now runs    
  36.    about 50 percent faster than that in version 2.
  37.  
  38.    It is assumed that the image is the width of and no taller than the
  39.    screen, and that you will set the correct display mode. No checking
  40.    is done to see that the .PCX file is compatible with the mode you've set.
  41.    You do, however, have to pass in the Turbo GraphDriver as a parameter
  42.    for all but 256-color files, so that the data will be interpreted
  43.    correctly. (For mode $0D, pass in 'EGA' or 'VGA', and see the comment on
  44.    palettes, below.)
  45.  
  46.    For the CGA formats, the data is put into two buffers on the heap, from
  47.    where it can be moved into the two display memory banks. See SHOWCGA for
  48.    an example. You can of course alter the unit to move the data directly
  49.    into display memory, but there is no great saving in time.
  50.  
  51.    For EGA and VGA formats, the data is written to page 0 of the video
  52.    buffer. This can easily be changed by setting "page_addr" to a different
  53.    value. Three different techniques of hiding the image while it is being
  54.    written are demonstrated in SHOWEGA, SHOWVGA, and SHOW256. If for any
  55.    reason you don't want to do this, you will want to rewrite the palette-
  56.    interpretation routines as separate procedures so you can set the
  57.    palette before decoding the image data.
  58.  
  59.    References:
  60.    ~~~~~~~~~~
  61.    Richard F. Ferraro, "Programmer's Guide to the EGA and VGA Cards"
  62.    (Addison-Wesley, 1988).
  63.  
  64.    Richard Wilton, "Programmer's Guide to PC and PS/2 Video Systems"
  65.    (Microsoft, 1987).
  66.  
  67.    "Technical Reference Manual [for Paintbrush]" (Zsoft, 1988). The
  68.    information in this booklet is also found in a file distributed with
  69.    at least some versions of Microsoft/PC Paintbrush.
  70.  
  71.    Software:
  72.    ~~~~~~~~
  73.    Besides the various incarnations of Paintbrush (ZSoft and Microsoft),
  74.    the excellent Deluxe Paint II Enhanced (Electronic Arts) can also create
  75.    files in .PCX format. Other graphics programs have conversion utilities.
  76. *)
  77.  
  78. { ======================================================================= }
  79.  
  80. INTERFACE
  81.  
  82. uses DOS, GRAPH;
  83.  
  84. type    RGBrec = record
  85.                    redval, greenval, blueval: byte;
  86.                  end;
  87.  
  88. var     pcxfilename: pathstr;
  89.         file_error: boolean;
  90.         pal: palettetype;
  91.         RGBpal: array[0..15] of RGBrec;
  92.         RGB256: array[0..255] of RGBrec;
  93.         page_addr: word;
  94.         bytes_per_line: word;
  95.         buff0, buff1: pointer;
  96.  
  97.         { CGA display memory banks: }
  98.         screenbuff0: array[0..7999] of byte absolute $b800:$0000;
  99.         screenbuff1: array[0..7999] of byte absolute $b800:$2000;
  100.  
  101. const   page0 = $A000;           { EGA/VGA display segment }
  102.  
  103. procedure SETMODE(mode: byte);
  104. procedure SETREGISTERS(var palrec);
  105. procedure READ_PCX_FILE(gdriver: integer; pfilename: pathstr);
  106. procedure READ_PCX256(pfilename: pathstr);
  107.  
  108. {========================================================================}
  109.  
  110. IMPLEMENTATION
  111.  
  112. var     scratch, abuff0, abuff1: pointer;
  113.         is_CGA, is_VGA: boolean;
  114.         repeatcount: byte;
  115.         datalength: word;
  116.         columncount, plane, video_index: word;
  117.         regs: registers;
  118.  
  119. const   buffsize = 65521;   { Largest possible }
  120.  
  121. { -------------------------- BIOS calls --------------------------------- }
  122.  
  123. { For modes not supported by the BGI, use SetMode to initialize the
  124.   graphics. Since SetRGBPalette won't work if Turbo hasn't done the 
  125.   graphics initialization itself, use SetRegisters to change the colors 
  126.   in mode $13. }
  127.  
  128. procedure SETMODE(mode: byte);
  129.  
  130. begin
  131. regs.ah:= 0;                 { BIOS set mode function }
  132. regs.al:= mode;              { Display mode }
  133. intr($10, regs);             { Call BIOS }
  134. end;
  135.  
  136. procedure SETREGISTERS(var palrec);
  137.  
  138. { Palrec is any string of 768 bytes containing the RGB data. }
  139.  
  140. begin
  141. regs.ah:= $10;               { BIOS color register function }
  142. regs.al:= $12;               { Subfunction }
  143. regs.es:= seg(palrec);       { Address of palette info. }
  144. regs.dx:= ofs(palrec);
  145. regs.bx:= 0;                 { First register to change }
  146. regs.cx:= $100;              { Number of registers to change }
  147. intr($10, regs);             { Call BIOS }
  148. end;
  149.  
  150. { ====================== EGA/VGA 16-color files ========================= }
  151.  
  152. procedure DECODE_16; assembler;
  153.  
  154. asm
  155.  
  156. (* Registers used:
  157.  
  158.    AL   data byte to be written to video
  159.    AH   data bytes per scan line
  160.    BX   end of input buffer
  161.    CL   number of times data byte is to be written
  162.    DL   current column in scan line
  163.    ES   output segment
  164.    DI   index into output buffer
  165.    DS   segment of input buffer
  166.    SI   index into input buffer
  167.    BP   current color plane
  168. *)
  169.  
  170. push    bp
  171.  
  172. { ----------------- Assembler procedure for 16-color files -------------- }
  173.  
  174. { The first section is initialization done on each run through the 
  175.   input buffer. }
  176.  
  177. @startproc:
  178. mov     bp, plane           { plane in BP }
  179. mov     es, page_addr       { video display segment }
  180. mov     di, video_index     { index into video segment }
  181. mov     ah, byte ptr bytes_per_line  { line length in AH }
  182. mov     dx, columncount     { column counter }
  183. mov     bx, datalength      { no. of bytes to read }
  184. xor     cx, cx              { clean up CX for loop counter }
  185. mov     cl, repeatcount     { count in CX }
  186. push    ds                  { save DS }
  187. lds     si, scratch         { input buffer pointer in DS:SI }
  188.  { We have to adjust datalength for comparison with SI. TP 6.0 pointers are
  189.    normalized, but the offset can still be 0 or 8. }
  190. add     bx, si
  191. cld                         { clear DF for stosb }
  192. cmp     cl, 0               { was last byte a count? }
  193. jne     @multi_data         { yes, so next is data }
  194. jmp     @getbyte            { no, so find out what next is }
  195.  
  196. { -------------- Procedure to write EGA/VGA image to video -------------- }
  197.  
  198. { The data in the .PCX file is organized by color plane, by line; that is,
  199.   all the data for plane 0 for line 1, then for plane 1, line 1, etc.
  200.   Writing the data to display memory is just a matter of masking out the
  201.   other planes while one plane is being written to. This is done with the
  202.   map mask register in the sequencer. All the other weird and wonderful
  203.   registers in the EGA/VGA do just fine with their default settings, thank
  204.   goodness. }
  205.  
  206. @writebyte:
  207. stosb                       { AL into ES:DI, inc DI }
  208. inc     dl                  { increment column }
  209. cmp